package org.feuyeux.loom;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import static org.openjdk.jmh.annotations.Mode.AverageTime;
import static org.openjdk.jmh.annotations.Mode.Throughput;

@BenchmarkMode({AverageTime,Throughput})
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class HelloLoomBenchmark {
    @Benchmark
    public void thread(Blackhole blackhole) {
        try (ExecutorService e = Executors.newCachedThreadPool()) {
            for (int i = 0; i < 100; i++) {
                final int x = i;
                e.submit(() -> {
                    String s = x + "\t";
                    blackhole.consume(s);
                });
            }
        }
    }

    @Benchmark
    public void virtualThread(Blackhole blackhole) {
        try (ExecutorService e = Executors.newVirtualThreadPerTaskExecutor()) {
            for (int i = 0; i < 100; i++) {
                final int x = i;
                e.submit(() -> {
                    String s = x + "\t";
                    blackhole.consume(s);
                });
            }
        }
    }
}
